<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\common\model\housing;

use app\common\model\BaseModel;
use app\common\model\property\basic\PropertyBasicModel;
use app\common\repositories\housing\HousingFacilityAccessRepository;

class HousingResourceModel extends BaseModel
{
    protected $autoWriteTimestamp = 'datetime';
    protected $createTime = 'add_time';
    protected $updateTime = false;

    public static function tablePk(): string
    {
        return 'id';
    }

    public static function tableName(): string
    {
        return 'housing_resource';
    }

    public function propertyInfo()
    {
        return $this->hasOne(PropertyBasicModel::class, 'id', 'property_id');
    }

    public function pictureInfo()
    {
        return $this->hasMany(HousingPictureModel::class, 'housing_id', 'id');
    }

    public function getPictureAttr()
    {
        return get_arr_column($this->pictureInfo()->field('image_url')->select()->toArray(), 'image_url');
    }

    public function getLabelIdsAttr()
    {
        return HousingResourceLabelModel::where('housing_id', $this->id)->column('label_id');
    }

    public function getLabelListAttr()
    {
        if ($this->LabelIds) {
            return HousingLabelModel::whereIn('id', $this->LabelIds)->field('id,label_name')->select()->toArray();
        }
        return [];
    }

    public function getFacilityAttr()
    {
        /** @var HousingFacilityAccessRepository $housingFacilityAccessRepository */
        $housingFacilityAccessRepository = app()->make(HousingFacilityAccessRepository::class);
        return $housingFacilityAccessRepository->getFacilityList($this->id);
    }

}
